#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TeacherProjectItemProvider : CY.GFive.Core.Providers.DALProvider.ITeacherProjectItemProvider
	{
		#region Sql
		private static readonly string SqlSelectTeacherProjectItem = "SELECT [Id], [ProjectItemCode],[TeacherCode] FROM [TeacherProjectItem] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTeacherProjectItem = "INSERT INTO [TeacherProjectItem]([ProjectItemCode],[TeacherCode]) values(@ProjectItemCode,@TeacherCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTeacherProjectItem = "UPDATE [TeacherProjectItem] SET [ProjectItemCode] = @ProjectItemCode,[TeacherCode] = @TeacherCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTeacherProjectItem = "DELETE [TeacherProjectItem] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTeacherProjectItem = "SELECT [Id], [ProjectItemCode],[TeacherCode] FROM [TeacherProjectItem]";
		#endregion

		#region ITeacherProjectItemProvider Members
		public Core.Business.TeacherProjectItem Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTeacherProjectItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TeacherProjectItem teacherProjectItem = new Core.Business.TeacherProjectItem();

				if (!reader.IsDBNull(0)) teacherProjectItem.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) teacherProjectItem.ProjectItemCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) teacherProjectItem.TeacherCode = reader.GetString(2);

				return teacherProjectItem;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TeacherProjectItem teacherProjectItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ProjectItemCode", SqlDbType.NVarChar, teacherProjectItem.ProjectItemCode);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, teacherProjectItem.TeacherCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTeacherProjectItem);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) teacherProjectItem.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TeacherProjectItem teacherProjectItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ProjectItemCode", SqlDbType.NVarChar, teacherProjectItem.ProjectItemCode);
			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, teacherProjectItem.TeacherCode);
			sql.AddParameter("@Id", SqlDbType.Int, teacherProjectItem.Id);
			sql.ExecuteSql(SqlUpdateTeacherProjectItem);
		}

		public void Delete(Core.Business.TeacherProjectItem teacherProjectItem)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teacherProjectItem.Id);
			sql.ExecuteSql(SqlDeleteTeacherProjectItem);
		}

		public IList<Core.Business.TeacherProjectItem> GetAllTeacherProjectItem()
		{
			IList<Core.Business.TeacherProjectItem> teacherProjectItemlist = new List<Core.Business.TeacherProjectItem>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTeacherProjectItem);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TeacherProjectItem teacherProjectItem = new Core.Business.TeacherProjectItem();

					if (!reader.IsDBNull(0)) teacherProjectItem.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) teacherProjectItem.ProjectItemCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) teacherProjectItem.TeacherCode = reader.GetString(2);

					teacherProjectItem.MarkOld();
					teacherProjectItemlist.Add(teacherProjectItem);
				}
				reader.Close();
			}
			return teacherProjectItemlist;
		}
		#endregion
	}
}
